<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                    <el-form-item label="设备名称" prop="name">
                        <el-input v-model="formData.name" clearable placeholder="请输入名称" />
                    </el-form-item>
                    <el-form-item label="设备编号" prop="device_no">
                        <el-input
                            v-model="formData.device_no"
                            clearable
                            placeholder="请输入设备编号"
                        />
                    </el-form-item>
                    <el-form-item label="设备类型" required prop="type">
                        <el-radio-group v-model="formData.type">
                            <el-radio :label="1">水表</el-radio>
                            <el-radio :label="2">电表</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="院区信息" prop="cid">
                        <el-select class="w-[280px]" v-model="formData.cid">
                            <el-option label="全部" value />
                            <el-option
                                v-for="item in optionsData.campus"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            />
                        </el-select>
                    </el-form-item>

                    <el-form-item label="排序">
                        <el-input-number v-model="formData.sort" :min="1" />
                    </el-form-item>
                    <el-form-item label="状态" required prop="status">
                        <el-radio-group v-model="formData.status">
                            <el-radio :label="1">显示</el-radio>
                            <el-radio :label="0">隐藏</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="deviceEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiDeviceAdd, apiDeviceEdit, apiDeviceDetail } from '@/api/device'
import { campusAll } from '@/api/department'

import { timeFormat } from '@/utils/util'
import { useDictOptions } from '@/hooks/useDictOptions'

import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑设备' : '新增设备表'
})

// 表单数据
const formData = reactive({
    id: '',
    cid: '',
    name: '',
    device_no: '',
    type: 1,
    bid: '',
    building: [],
    sort: 1,
    status: 1
})

const { optionsData } = useDictOptions<{
    campus: any[]
}>({
    campus: {
        api: campusAll
    }
})

// 表单验证
const formRules = reactive<any>({
    building: [
        {
            required: true,
            message: '请选择院区楼栋',
            trigger: ['blur']
        }
    ],
    name: [
        {
            required: true,
            message: '请输入名称',
            trigger: ['blur']
        }
    ],
    device_no: [
        {
            required: true,
            message: '请输入设备编号',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择类型',
            trigger: ['blur']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiDeviceDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' ? await apiDeviceEdit(data) : await apiDeviceAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
